/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    gpio.c
 * @brief   This file provides code for the configuration
 *          of all used GPIO pins.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "gpio.h"

/* USER CODE BEGIN 0 */
#define KEY_SHORT_CNT 10
#define KEY_LONG_CNT 300
/* USER CODE END 0 */

/*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/** Configure pins as
 * Analog
 * Input
 * Output
 * EVENT_OUT
 * EXTI
 */
void MX_GPIO_Init(void)
{

    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOF_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();

    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(GPIOA, MOSI_Pin | CLK_Pin | DC_Pin | CS_Pin | LCD_BL_Pin, GPIO_PIN_RESET);

    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(RES_GPIO_Port, RES_Pin, GPIO_PIN_RESET);

    /*Configure GPIO pins : PFPin PFPin */
    GPIO_InitStruct.Pin = KEY1_Pin | KEY2_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

    /*Configure GPIO pin : PtPin */
    GPIO_InitStruct.Pin = KEY3_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(KEY3_GPIO_Port, &GPIO_InitStruct);

    /*Configure GPIO pins : PAPin PAPin PAPin PAPin
                             PAPin */
    GPIO_InitStruct.Pin = MOSI_Pin | CLK_Pin | DC_Pin | CS_Pin | LCD_BL_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /*Configure GPIO pin : PtPin */
    GPIO_InitStruct.Pin = RES_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(RES_GPIO_Port, &GPIO_InitStruct);
}

/* USER CODE BEGIN 2 */
// u8 KEY_Scan(u8 mode) {
//     static char key_up = 1;  // 按键松开标志
//     if (mode == 1)
//         key_up = 1;  // 支持连按
//     if (key_up && (KEY0 == 0 || KEY1 == 0 || KEY2 == 0)) {
//         delay_ms(10);
//         key_up = 0;
//         if (KEY0 == 0)
//             return KEY0_PRES;
//         else if (KEY1 == 0)
//             return KEY1_PRES;
//         else if (KEY2 == 0)
//             return KEY2_PRES;
//     } else if (KEY0 == 1 && KEY1 == 1 && KEY2_PRES == 1)
//         key_up = 1;
//     return 0;  // 无按键按下返�??0
// }
u16 Press_time0;
u16 Press_time1;
u16 Press_time2;
Key_State KeyState;
void Key_Stat(void) // 1ms
{
    if (KEY0 == 0)
    {
        Press_time0++;
    }
    else
    {
        if ((Press_time0 > KEY_SHORT_CNT) && (Press_time0 < KEY_LONG_CNT))
        {
            KeyState.K0_S_Flag = 1;
            KeyState.K0_L_Flag = 0;
        }
        else if (Press_time0 >= KEY_LONG_CNT)
        {
            KeyState.K0_S_Flag = 0;
            KeyState.K0_L_Flag = 1;
        }
        Press_time0 = 0;
    }
    //------------------------------------------------------
    if (KEY1 == 0)
        Press_time1++;
    else
    {
        if ((Press_time1 > KEY_SHORT_CNT) && (Press_time1 < KEY_LONG_CNT))
        {
            KeyState.K1_S_Flag = 1;
            KeyState.K1_L_Flag = 0;
        }
        else if (Press_time1 >= KEY_LONG_CNT)
        {
            KeyState.K1_S_Flag = 0;
            KeyState.K1_L_Flag = 1;
        }

        Press_time1 = 0;
    }
    //---------------------------------------------------------
    if (KEY2 == 0)
    {
        Press_time2++;
    }
    else
    {
        if ((Press_time2 > KEY_SHORT_CNT) && (Press_time2 < KEY_LONG_CNT))
        {
            KeyState.K2_S_Flag = 1;
            KeyState.K2_L_Flag = 0;
        }
        else if (Press_time2 >= KEY_LONG_CNT)
        {
            KeyState.K2_S_Flag = 0;
            KeyState.K2_L_Flag = 1;
        }
        Press_time2 = 0;
    }
}

/* USER CODE END 2 */
